package com.huaxiafinance.management.util;


import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DateUtil
{

    private static final Logger logger = Logger.getLogger(DateUtil.class);

    private static final long MILLIS_IN_A_SECOND = 1000;

    private static final long SECONDS_IN_A_MINUTE = 60;

    /**
     * 时间格式
     */
    private static final String TIME_PATTERN = "yyyy/MM/dd HH:mm:ss";

    /**
     * 时间开始字符串
     */
    private static final String BEGIN_PATTERN = " 00:00:00";

    /**
     * 时间结束字符串
     */
    private static final String OVER_PATTERN = " 23:59:59";

    /**
     * 时间格式
     */
    private static final String TIME_PATTERN_TWO = "yyyy-MM-dd HH:mm:ss";

    private static final String MONTH_BEGIN = "yyyy/MM/dd 00:00:00";

    private static final String MONTH_OVER = "yyyy/MM/dd 23:59:59";

    /**
     * 获取当前日期数字类型
     *
     * @param format
     * @return String
     */
    public static String millisecondString(String format)
    {
        DateFormat dformat = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        return dformat.format(cal.getTimeInMillis());
    }

    /**
     * 将特定时间格式化
     *
     * @param date
     * @param format
     * @return String
     */
    public static String formatDate(Date date, String format)
    {
        DateFormat dformat = new SimpleDateFormat(format);
        return dformat.format(date);
    }

    /**
     * 格式化当前时间
     *
     * @param format
     * @return String
     */
    public static String formatCurrentDate(String format)
    {
        DateFormat dformat = new SimpleDateFormat(format);
        return dformat.format(new Date());
    }

    /**
     * 字符串转换为日期
     *
     * @param format
     *            需要和字符串格式保持一致
     * @param dateString
     *            日期时间
     * @return Date
     * @throws Exception
     */
    public static Date stringToDate(String format, String dateString)
        throws Exception
    {
        DateFormat dformat = new SimpleDateFormat(format);
        return dformat.parse(dateString);
    }

    /**
     * 比较两个时间的大小
     *
     * @param dt1
     *            传入的时间参数1
     * @param dt2
     *            传入的时间参数2
     * @return int
     */
    public static int compareDate(Date dt1, Date dt2)
    {
        if (dt1.getTime() > dt2.getTime())
        {
            return 1;
        }
        else if (dt1.getTime() < dt2.getTime())
        {
            return -1;
        }
        else
        {// 相等
            return 0;
        }
    }

    /**
     * 输入日期加指定天数
     *
     * @param date
     * @param intBetween
     * @return Date
     */
    public static Date increaseDay(Date date, int intBetween)
    {
        Calendar calo = Calendar.getInstance();
        calo.setTime(date);
        calo.add(Calendar.DATE, intBetween);
        return calo.getTime();
    }

    /**
     * 比较两个时间先后
     *
     * @param str1
     *            传入的字符串
     * @param str2
     *            传入的字符串
     * @return int negative integer, zero, or a positive integer as str1 is less than, equal to, or
     *         greater than str2
     */
    public static int compareDate(String str1, String str2)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = null;
        Date date2 = null;
        try
        {
            date1 = formatter.parse(str1);
            date2 = formatter.parse(str2);
        }
        catch (Exception ex)
        {
            logger.error("错误++++++" + ex);
        }
        if (date1 != null && date2 != null)
        {
            return date1.compareTo(date2);
        }
        else
        {
            return 0;
        }
    }

    /**
     * 判断date1是否在date2之后
     *
     * @param date1
     * @param date2
     * @return boolean
     */
    public static boolean isDateAfter(Date date1, Date date2)
    {
        Date theDate1 = DateUtils.truncate(date1, Calendar.DATE);
        Date theDate2 = DateUtils.truncate(date2, Calendar.DATE);
        return theDate1.after(theDate2);
    }

    /**
     * 判断time1是否在time2之后，忽略日期部分
     *
     * @param time1
     * @param time2
     * @return boolean
     */
    public static boolean isTimeAfter(Date time1, Date time2)
    {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);
        calendar1.set(1900, 1, 1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);
        calendar2.set(1900, 1, 1);
        return calendar1.after(calendar2);
    }

    /**
     * 计算time2比time1晚多少分钟，忽略日期部分
     *
     * @param time1
     * @param time2
     * @return int
     */
    public static int getMinuteDiffByTime(Date time1, Date time2)
    {
        long startMil = 0;
        long endMil = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time1);
        calendar.set(1900, 1, 1);
        startMil = calendar.getTimeInMillis();
        calendar.setTime(time2);
        calendar.set(1900, 1, 1);
        endMil = calendar.getTimeInMillis();
        return (int)((endMil - startMil) / MILLIS_IN_A_SECOND / SECONDS_IN_A_MINUTE);
    }

    /**
     * 获取月初时间
     *
     * @return Date
     */
    public static Date getBeginMonthTime()
    {
        Date startDate = null;
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        startDate = c.getTime();
        return startDate;
    }

    /**
     * 时间格式化
     *
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return Map
     * @throws Exception
     */
    public static Map<String, Date> getTimeFormat(String startTime, String endTime)
        throws Exception
    {
        startTime = startTime + BEGIN_PATTERN;
        endTime = endTime + OVER_PATTERN;
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date beginTime = sdf.parse(startTime);
        Date overTime = sdf.parse(endTime);
        Map<String, Date> map = new HashMap<String, Date>();
        map.put("beginTime", beginTime);
        map.put("overTime", overTime);
        return map;
    }

    /**
     * 获取开始时间
     *
     * @param startTime
     * @return Date
     * @throws Exception
     */
    public static Date getStartTime(String startTime)
        throws Exception
    {
        startTime = startTime + BEGIN_PATTERN;
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date beginTime = sdf.parse(startTime);
        return beginTime;
    }

    /**
     * 获取结束时间
     *
     * @param endTime
     * @return Date
     * @throws Exception
     */
    public static Date getEndTime(String endTime)
        throws Exception
    {
        endTime = endTime + OVER_PATTERN;
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date overTime = sdf.parse(endTime);
        return overTime;
    }

    /**
     * 获取开始时间
     *
     * @param startTime
     * @return Date
     * @throws Exception
     */
    public static Date getStart(String startTime)
        throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN_TWO);
        Date beginTime = sdf.parse(startTime);
        return beginTime;
    }

    /**
     * 获取结束时间
     *
     * @param endTime
     * @return Date
     * @throws Exception
     */
    public static Date getEnd(String endTime)
        throws Exception
    {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN_TWO);
        Date overTime = sdf.parse(endTime);
        return overTime;
    }

    /**
     * 时间格式化
     *
     * @param startTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return Map<String, Date>
     * @throws Exception
     */
    public static Map<String, Date> getTimeFormatTo(String startTime, String endTime)
        throws Exception
    {
        startTime = startTime + BEGIN_PATTERN;
        endTime = endTime + OVER_PATTERN;
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN_TWO);
        Date beginTime = sdf.parse(startTime);
        Date overTime = sdf.parse(endTime);
        Map<String, Date> map = new HashMap<String, Date>();
        map.put("beginTime", beginTime);
        map.put("overTime", overTime);
        return map;
    }

    /**
     * 取当月的最后一天
     *
     * @return Date
     */
    public static Date getLastDayOfMonth()
    {
        Date dates = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
        String monthDate = sdf2.format(dates);
        int year = Integer.parseInt(monthDate.substring(0, 4));
        int month = Integer.parseInt(monthDate.substring(5, monthDate.length() - 1));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
        Date date = cal.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat(MONTH_OVER);
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date monthLastDate = null;
        String n = formatter.format(date);
        try
        {
            monthLastDate = sdf.parse(n);
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        return monthLastDate;
    }

    /**
     * 取当月的第一天
     *
     * @return Date
     */
    public static Date getFirstDayOfMonth()
    {
        Date dates = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月");
        String monthDate = sdf2.format(dates);
        int year = Integer.parseInt(monthDate.substring(0, 4));
        int month = Integer.parseInt(monthDate.substring(5, monthDate.length() - 1));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        Date date = cal.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat(MONTH_BEGIN);
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date monthFirstDate = null;
        try
        {
            monthFirstDate = sdf.parse(formatter.format(date));
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        return monthFirstDate;
    }

    /**
     * 取当周的第一天
     *
     * @return Date
     */
    public static Date getFirstDayOfWeek()
    {
        Date dates = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_PATTERN);
        String monthDate = sdf2.format(dates);
        Calendar cal = Calendar.getInstance();
        try
        {
            cal.setTime(new SimpleDateFormat(TIME_PATTERN).parse(monthDate));
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1)
        {
            d = -6;
        }
        else
        {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
        String data1 = new SimpleDateFormat(MONTH_BEGIN).format(cal.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date date = null;
        try
        {
            date = sdf.parse(data1);
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        return date;
    }

    /**
     * 取当周的最后一天
     *
     * @return Date
     */
    public static Date getLastDayOfWeek()
    {
        Date dates = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_PATTERN);
        String monthDate = sdf2.format(dates);
        Calendar cal = Calendar.getInstance();
        try
        {
            cal.setTime(new SimpleDateFormat(TIME_PATTERN).parse(monthDate));
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1)
        {
            d = -6;
        }
        else
        {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
        cal.add(Calendar.DAY_OF_WEEK, 6);
        // 所在周结束日期
        String data2 = new SimpleDateFormat(TIME_PATTERN).format(cal.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
        Date date = null;
        try
        {
            date = sdf.parse(data2);
        }
        catch (ParseException e)
        {
            logger.error("错误++++++" + e);
        }
        return date;
    }
}
